<template>
  <el-config-provider :locale="zhCn">
    <router-view v-slot="{ Component }">
      <i-xyz-transition :duration="'3'" appear>
        <component :is="Component" v-if="Component" />
      </i-xyz-transition>
    </router-view>
  </el-config-provider>
</template>
<script lang="ts" setup>
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
import { computed, onMounted } from "vue";
import { getUserCompany, getUserMenu } from "~/api/system/user";
import { useMenuStore } from "~/store/modulers/menu";
import { storeToRefs } from "pinia";
import router from "~/router";
import { useRoute } from "vue-router";
import IXyzTransition from "~/icesoft/components/transitions/IXyzTransition.vue";
import { useUserStore } from "~/store/modulers/user";

const route = useRoute();
const userStore = useUserStore();
const { token } = storeToRefs(userStore);
const key = computed(() => {
  return route.path + Math.random();
});

const menuStore = useMenuStore();
const { defaultActive } = storeToRefs(menuStore);

function onLoadHandler() {
  const token = localStorage.getItem("token");
  if (token) {
    getUserCompany().then(res =>{
      if (res.data) {
        userStore.setCompany(res.data)
      }
    })
    getUserMenu().then(res => {
      if (res.data) {
        menuStore.refreshMenu(res.data);
      }
    });
    if (defaultActive?.value) {
      router.push(defaultActive?.value);
    }
  }
}

onMounted(() => {
  if (!token?.value) {
    router.push("/login");
  }
  window.addEventListener("load", e => onLoadHandler());
});
</script>

<style scoped>
#app {
    text-align: center;
}
</style>
